package lex.mtd.core.net;

import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import org.lex.net.log.Log;
import org.lex.utils.ToString;


public class HttpUtils
{
	static void setRequests( URLConnection con )
	{
		URL url = con.getURL();
		con.setRequestProperty( "GET", url.getFile() + " HTTP/1.1" );
		con.setRequestProperty( "Accept", "*/*" );
		con.setRequestProperty( "Cache-Control", "no-cache" );
		con.setRequestProperty( "Connection", "close" );
		con.setRequestProperty( "Host", url.getHost() );
		con.setRequestProperty( "Pragma", "no-cache" );
		con.setRequestProperty( "Referer", url.getProtocol() + "://" + url.getHost() );
		con.setRequestProperty( "User-Agent",
			" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; )" );
	}

	static void setSeek( URLConnection con, long seek )
	{
		StringBuilder buf = new StringBuilder( "bytes=" );
		buf.append( seek ).append( '-' );
		con.setRequestProperty( "Range", buf.toString() );
	}

	static void log( Log log, Object source, Log.Type type, Map<String, List<String>> map )
	{
		StringBuilder buf = new StringBuilder();
		for( Map.Entry<String, List<String>> e : map.entrySet() )
		{
			buf.setLength( 0 );
			buf.append( e.getKey() ).append( ": " );
			ToString.format( e.getValue().iterator(), buf, "", "", ", " );
			log.log( type, source, buf.toString() );
		}
	}
}
